package com.junci.mtest.views.activity;

import android.graphics.Typeface;
import android.os.Bundle;

import com.bumptech.glide.Glide;

import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;


import com.junci.mtest.R;

import com.junci.mtest.databinding.ActivityPlanBinding;
import com.junci.mtest.model.bean.TestPlanBean;
import com.junci.mtest.utils.DisplayUtil;
import com.junci.mtest.utils.StringUtil;
import com.junci.mtest.utils.VideoSurfaceView;
import com.junci.mtest.views.BaseActivity;
import com.junci.mtest.views.adapter.PlanActivityEduAdapter;
import com.junci.mtest.vm.PlanClickHandler;
import com.junci.mtest.vm.PlanViewModel;

import java.util.ArrayList;

public class PlanActivity extends BaseActivity {
    private ActivityPlanBinding binding;

    private ArrayList<TabLayout.Tab> DayTabs ;//日期tabs
    PlanActivityEduAdapter adapter ;
    PlanViewModel viewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_plan);
        viewModel = new ViewModelProvider(this).get(PlanViewModel.class);
        binding.setViewModel(viewModel);
        TestPlanBean.Data.Rows  rows = (TestPlanBean.Data.Rows) getIntent().getSerializableExtra("data");
        viewModel.setItemData(rows);
        viewModel.getItem().observe(this, this::initView);
        PlanClickHandler onclickHandler = new PlanClickHandler();
        binding.setClickHandler(onclickHandler);
    }

    private void initView(TestPlanBean.Data.Rows data){
        binding.tvPlanName.setText(data.getName());
        Glide.with(this).load(data.getPic_url()).into(binding.ivTop);
        if (!StringUtil.isEmpty(data.getVideo_url())) {
            binding.videoview.setCallback(new VideoSurfaceView.playCallback() {
                @Override
                public void onPlay() {
                    binding.ivTop.setVisibility(View.GONE);
                }
            });
            binding.videoview.setVideoPath(data.getVideo_url());
            binding.videoview.setlooping(true);
        }
        binding.tvContent.setText(data.getContent()+"");
        binding.tvGoing.setText(data.getGoing()+"");
        binding.tvTarget.setText(data.getTarget()+"");
        binding.tvNum.setText(data.getLessonNum()+"节");
        binding.tvDuration.setText(data.getDuration()+"天");
        setEduList(data);
    }
    /**
     * 训练打卡list
     * @param data
     *
     */
    private void setEduList(TestPlanBean.Data.Rows data){
        DayTabs = new ArrayList<>();
        binding.tableDay.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                binding.vpEdus.setCurrentItem(tab.getPosition(),true);
                TextView tv_title =  (TextView)tab.getCustomView().findViewById(R.id.tv_title);
                tv_title.setSelected(true);
                tv_title.setTypeface(null, Typeface.BOLD);
                tv_title.setTextColor(getResources().getColor(R.color.red));
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tv_title =  (TextView)tab.getCustomView().findViewById(R.id.tv_title);
                tv_title.setSelected(false);
                tv_title.setTypeface(null,Typeface.NORMAL);
                tv_title.setTextColor(getResources().getColor(R.color.black));
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });


        for (TestPlanBean.Data.Rows.Edus edus : data.getEdus()) {
//                DayTabs.add(binding.tableDay.newTab().setText(edus.getDate()));
            TabLayout.Tab tab = binding.tableDay.newTab().setText(edus.getDate()).setCustomView(R.layout.tab_plan_edu);
            DayTabs.add(tab);
            binding.tableDay.addTab(tab);
            if (tab.getPosition()==0) {
                tab.select();
            }
            TextView tv_title =  (TextView)tab.getCustomView().findViewById(R.id.tv_title);
            tv_title.setText(edus.getDate());
        }

        adapter = new PlanActivityEduAdapter(this,viewModel);
//        binding.listSign.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
//        binding.listSign.setAdapter(adapter);
        binding.vpEdus.setAdapter(adapter);
        binding.vpEdus.setOffscreenPageLimit(2);
        binding.vpEdus.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.tableDay.selectTab(DayTabs.get(position));
                adapter.setSelectPostion(position);
            }
        });
        RecyclerView recyclerView =  (RecyclerView)binding.vpEdus.getChildAt(0);
        recyclerView.setPadding(30,0, DisplayUtil.getWidth()/3,0);
        recyclerView.setClipToPadding(false);
        adapter.setItems(data.getEdus());
    }




}